﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using DragDraw.UserService;
using DragDraw;

namespace DragDraw.Util
{
    public class WidgetSpaceProxy
    {
        private int m_widgetID;

        internal WidgetSpaceProxy(int widgetID)
        {
            m_widgetID = widgetID;
        }
        public void ShowModalDialog(UserControl widget, string title)
        {
            OperationManager.Instance.
                CurrentSpaceManager.ShowModalDialog(widget, title);
        }
        public void HideModalDialog()
        {
            OperationManager.Instance.
                CurrentSpaceManager.ShowModalDialog(null);
        }

        /// <summary>
        /// 设定空间的背景显示控件
        /// </summary>
        /// <param name="background">用于覆盖背景的控件</param>
        public void SetSpaceBackground(UserControl background)
        {
        }

        /// <summary>
        /// 在工具栏显示一条信息
        /// </summary>
        /// <param name="message">信息内容</param>
        public void ShowMessage(string message)
        {
        }
    }

    public class WidgetServiceProxy :
        ISessionServiceListener,
        IDuplexSessionServiceListener,
        IFileServiceListener
    {
        private int m_widgetID;

        internal WidgetServiceProxy(int widgetID)
        {
            m_widgetID = widgetID;
        }

        /// <summary>
        /// 获取小工具的会话，会触发GetSessionCompleted事件
        /// </summary>
        /// <param name="sessionName">会话名称</param>
        public void GetSession(string sessionName)
        {
            ServiceManager.Instance.GetSession(m_widgetID, sessionName, this);
        }

        /// <summary>
        /// 设定小工具的会话值，会触发SetSessionCompleted事件
        /// </summary>
        /// <param name="sessionName">会话名称</param>
        /// <param name="value">会话值</param>
        public void SetSession(string sessionName, string value)
        {
            ServiceManager.Instance.SetSession(m_widgetID, sessionName, value, this);
        }

        /// <summary>
        /// 读取指定的文件
        /// </summary>
        /// <param name="filename"></param>
        public void LoadFile(string filename)
        {
            ServiceManager.Instance.LoadFile(filename, this);
        }
        public void SaveFile(string filename)
        {
            ServiceManager.Instance.SaveFile(filename, this);
        }
        public void JoinDuplexSession(string sessionName)
        {
            ServiceManager.Instance.JoinSession(
                OperationManager.Instance.MyUserInfo.UserID,
                m_widgetID, sessionName, this);
        }
        public void UpdateDuplexSession(string sessionName, string value)
        {
            SessionData s = new SessionData()
            {
                UserID = OperationManager.Instance.MyUserInfo.UserID,
                WidgetID = m_widgetID,
                SessionName = sessionName,
                Value = value
            };
            ServiceManager.Instance.UpdateSession(s, this);
        }
        public event EventHandler<GetSessionCompletedEventArgs> GetSessionCompleted;
        public event EventHandler<SetSessionCompletedEventArgs> SetSessionCompleted;
        public event EventHandler<OpenReadCompletedEventArgs> LoadFileCompleted;
        public event EventHandler<OpenWriteCompletedEventArgs> SaveFileCompleted;
        public event EventHandler<OnUpdateSessionEventArgs> OnDuplexSessionUpdate;
        public void OnGetSession(GetSessionCompletedEventArgs e)
        {
            if (GetSessionCompleted != null)
                GetSessionCompleted(this, e);
        }
        public void OnSetSession(SetSessionCompletedEventArgs e)
        {
            if (SetSessionCompleted != null)
                SetSessionCompleted(this, e);
        }
        public void OnLoadFile(OpenReadCompletedEventArgs e)
        {
            if (LoadFileCompleted != null)
                LoadFileCompleted(this, e);
        }
        public void OnSaveFile(OpenWriteCompletedEventArgs e)
        {
            if (SaveFileCompleted != null)
                SaveFileCompleted(this, e);
        }
        public void OnUpdateSession(OnUpdateSessionEventArgs e)
        {
            if (OnDuplexSessionUpdate != null)
                OnDuplexSessionUpdate(this, e);
        }
    }
}
